package com.ysd.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ysd.crm.entity.LayuiTree;
import com.ysd.crm.entity.Modules;
import com.ysd.crm.mapper.ModulesMapper;
import com.ysd.crm.search.ModulesSearch;
import com.ysd.crm.service.ModulesService;
import com.ysd.crm.utils.Result;

@Service
public class ModulesServiceImpl implements ModulesService {
	@Autowired
	private ModulesMapper MM;

	@Override
	public List<LayuiTree> getLayuiModuleTree(LayuiTree l) {
		List<Modules> list = MM.getAll();
		List<LayuiTree> layuiList = getLayuiList(list,l);
		List<LayuiTree> tree = getLayuiTreeList(layuiList);
		return tree;
	}

	/**
	 * 获取layuitree实体类的数据格式
	 * 
	 * @param mList
	 * @return
	 */
	public List<LayuiTree> getLayuiList(List<Modules> mList,LayuiTree l) {
		List<LayuiTree> layList = new ArrayList<>();
		for (Modules m : mList) {
			layList.add(new LayuiTree(m.getName(), m.getId(), m.getParentId(), null, null, m.getPath(), true, false,
					false));
		}
		return layList;
	}

	/**
	 * 获得主模块
	 * 
	 * @param layuiList
	 * @return
	 */
	public List<LayuiTree> getLayuiTreeList(List<LayuiTree> layuiList) {
		List<LayuiTree> tree = new ArrayList<LayuiTree>();
		for (LayuiTree t : layuiList) {
			if (t.getPid() == 0) {
				tree.add(getLayuiTreeChildren(t, layuiList));
			}
		}
		return tree;
	}

	/**
	 * 获得子模块
	 * 
	 * @param t
	 * @param layuiList
	 * @return
	 */
	public LayuiTree getLayuiTreeChildren(LayuiTree t, List<LayuiTree> layuiList) {
		for (LayuiTree item : layuiList) {
			if (t.getId().equals(item.getPid())) {
				if (t.getChildren() == null) {
					t.setChildren(new ArrayList<LayuiTree>());
				}
				t.getChildren().add(getLayuiTreeChildren(item, layuiList));
			}
		}
		return t;
	}

	@Override
	public Result add(ModulesSearch ms) {
		// TODO Auto-generated method stub
		int i = MM.getModuleByName(new Modules(ms.getId(), ms.getName(), ms.getParentId(), null, null));
		if (i > 0) {
			return Result.toClient(false, "添加失败,模块不可重复");
		}
		int j = MM.add(new Modules(null, ms.getName(), ms.getParentId(), ms.getPath(), ms.getWeight()));
		if (j > 0) {
			return Result.toClient(true, "添加成功", "");
		}
		return Result.toClient(false, "添加失败", "");

	}

	@Override
	public Result update(ModulesSearch ms) {
		int i = MM.getModuleByName(new Modules(ms.getId(), ms.getName(), ms.getParentId(), null, null));
		if (i > 1) {
			return Result.toClient(false, "修改失败,该模块名称已存在");
		}
		int j = MM.update(new Modules(ms.getId(), ms.getName(), ms.getParentId(), ms.getPath(), ms.getWeight()));
		if (j > 0) {
			return Result.toClient(true, "修改成功", "");
		}
		return Result.toClient(false, "修改失败", "");
	}

	@Override
	public Result delete(ModulesSearch ms) {
		// TODO Auto-generated method stub
		int i = MM.getModuleUseNum(ms.getId());
		if (i > 0) {
			return Result.toClient(false, "存在角色使用该模块,禁止删除");
		}
		int j = MM.delete(new Modules(ms.getId(), ms.getName(), ms.getParentId(), ms.getPath(), ms.getWeight()));
		if (j > 0) {
			return Result.toClient(true, "删除成功");
		}
		return Result.toClient(false, "删除失败");
	}

	@Override
	public List<Modules> getAll(LayuiTree l) {
		// TODO Auto-generated method stub
		return MM.getAll();
	}

	@Override
	public List<Modules> getModulesByUid(String uid) {
		// TODO Auto-generated method stub
		return MM.getModulesByUid(uid);
	}

	@Override
	public List<LayuiTree> getModuleByRid(String rid) {
		// TODO Auto-generated method stub
		List<String> modulesByRid = MM.getModulesByRid(rid);
		List<Modules> all = MM.getAll();
		List<LayuiTree> list = setLayuiTreeList(modulesByRid, all);
		List<LayuiTree> layuiTreeList = getLayuiTreeList(list);
		return layuiTreeList;
	}

	public List<LayuiTree> setLayuiTreeList(List<String> rm,List<Modules> all){
		List<LayuiTree> ltList = new ArrayList<LayuiTree>();
		for (Modules m : all) {
			LayuiTree l = new LayuiTree(m.getName(), m.getId(), m.getParentId(), null, null, null, false, false, false);
			for (String rmitem : rm) {
				if(m.getId().toString().equals(rmitem)) {
					l.setChecked(true);
				}
			}
			ltList.add(l);
		}
		return ltList;
	}

	@Override
	public Result addRoleModulesByRidAndMids(String rid, String mid) {
		// TODO Auto-generated method stub
		List<String> mids = MM.getMidsByMid(mid);
		int i = MM.addModuleByRid(rid, mids);
		if(i==0) {
			return Result.toClient(false, "添加失败");
		}
		return Result.toClient(true, "添加成功");
	}

	@Override
	public Result delRoleModulesByRidAndMids(String rid, String mid) {
		// TODO Auto-generated method stub
		List<String> mids = MM.getMidsByMid(mid);
		int i = MM.delModuleByRid(rid, mids);
		if(i==0) {
			return Result.toClient(false, "删除失败");
		}
		return Result.toClient(true, "删除成功");
	}
}
